layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table'], function () {
    var form = layui.form,
        table = layui.table,
        admin = layui.admin;

    /**
     * 防重复设置
     * @type {boolean}
     */
    var edit_flag = true;

    table.render({
        elem: '#enterprise_Store_table'
        , url: '/enterprise_store/queryData'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'id', width: 100, title: 'id', "align": "center"}
            , {field: 'oemId', title: 'oemId', "align": "center", hide: true}
            , {field: 'userId', title: 'userId', "align": "center", hide: true}
            , {field: 'enterpriseId', title: '资料Id', "align": "center", hide: true}
            , {field: 'name', width: 200, title: '门店名', "align": "center"}
            , {field: 'createdTime', width: 240, title: '注册时间', "align": "center"}
            ,{field:'type', width:140, title: '门店等级',"align":"center",templet:function (d){
                    if(d.type == 1){
                        return "优惠券门店";
                    }else {
                        return "普通门店";
                    }
                }}
            , {field: 'OPT', width: 200, title: '操作', "align": "center", toolbar: "#OptData"}
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {

        }
        , id: 'enterprise_Store_table'
        , page: true
    });

    table.on('tool(enterprise_Store_table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (layEvent === 'modify_enterpriseStore') { //编辑门店名称
            admin.popup({
                title: '编辑'
                , shade: 0
                , anim: -1
                , offset: "20px"
                , area: ['80%', '500px']
                , skin: 'layui-anim layui-anim-upbit'
                , content: $("#add_enterpriseStore_win")
                , end: function () {
                    table.reload('enterprise_Store_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {}
                    });
                }
            });

            form.val('add_enterpriseStore_form', {
                "id": data.id,
                "name": data.name,
            })
        } else if(layEvent === 'upgrade'){
            layer.confirm('是否将：' + data.name + "升级成为优惠券门店?", function (index) {
                $.ajax({
                    url: '/enterprise_store/upgradeStore',
                    type: 'post',
                    data: {'id': data.id},
                    success: function (suc) {
                        if (suc.code == 1) {
                            layer.close(index);
                            layer.msg(suc.message, {icon: 1});
                            table.reload('enterprise_Store_table', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                                , where: {}
                            });
                        } else {
                            layer.msg(suc.message, {icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    });


    //添加商户
    $(".enterpriseStoreAdd_btn").click(function () {

        $("#add_enterpriseStore_form")[0].reset();
        /**
         * 初始化 等级下拉
         */
        admin.popup({
            title: '编辑'
            , shade: 0
            , anim: -1
            , offset: "20px"
            , area: ['80%', '500px']
            , skin: 'layui-anim layui-anim-upbit'
            , content: $("#add_enterpriseStore_win")
            , end: function () {
                table.reload('enterprise_Store_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {}
                });
            }
        });

        form.val('add_enterpriseStore_form', {
            "id": ""
        })

    });

    /**
     * 编辑门店名
     */
    form.on('submit(addenterpriseStoreOk)', function (data) {
        $.ajax({
            url: "/enterprise_store/edit",
            data: data.field,
            type: "post",
            success: function (result) {
                if (result.code == 1) {
                    layer.msg("编辑成功", {
                        offset: '32%'
                        , icon: 1
                        , time: 0
                        , btn: '知道了'
                        , btnAlign: "c"
                        , shade: 0.4
                        , yes: function () {
                            table.reload('enterprise_Store_table', {});
                            layer.closeAll();
                        }
                    })
                } else {
                    layer.msg(result.message, {
                        offset: '32%'
                        , icon: 2
                        , time: 3000
                    }, function () {
                        layer.closeAll();
                    })
                }
            }
        });
        return false;
    });

    $(".search_btn").click(function () {
        table.reload('enterprise_Store_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                name: $(".queryTable input[name='name']").val()
            }
        });
    });

});

